<?php
require('SAM/php_sam.php');
define("SAM_HOST" , "SAM_HOST");
define("SAM_PORT" , "SAM_PORT");
define("SAM_WAIT" ,"SAM_WAIT" );
define("SAM_MQTT_QOS" ,"SAM_MQTT_QOS" );
//create a new connection object
$conn = new SAMConnection();

//start initialise the connection
$conn->connect(SAM_MQTT, array( SAM_HOST => "test.mosquitto.org",
                                 SAM_PORT => 1883));

//subscribe to topic cpu
    $subUp = $conn->subscribe('topic://cpupc-asitha',array( SAM_WAIT => 1, SAM_MQTT_QOS => 0)) OR die('could not subscribe');

//print confirmation to terminal
 echo "subscribed";
$i=0;
while($conn)
{
		
		
		/* $ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch); */


       //receive latest message on topic $subUp
       $msgUp = $conn->receive($subUp);
	   //if($msgUp==true)
			/* echo $conn->errno;
			echo $conn->error;
			echo $conn->debug; */
       //if there is a message
      if (!$msgUp->body=="")
      {
          //echo message to terminal
          echo $msgUp->body;
      }

      //wait 1s
      sleep(3);
	//  $i++;
}

?>